package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.*;
import cn.edu.nju.software.cdiy.dao.HcommentDao;
import cn.edu.nju.software.cdiy.dao.ValueDao;
import cn.edu.nju.software.cdiy.util.BusinessConstant;
import cn.edu.nju.software.cdiy.util.Constant;

public class HcommentDaoImpl implements HcommentDao {

	@Autowired
	private SessionFactory sessionFactory;

	@Autowired
	private ValueDao valueDao;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.nju.software.cdiy.dao.HcommentDao#loadByUserId(long)
	 * 某个用户的硬件评论列表
	 */
	@Override
	public List<Hcomment> getUserComments(long userId) {
		Session session = sessionFactory.getCurrentSession();

		List<Hcomment> list = null;
		try {

			Query query = session
					.createQuery("from Hcomment h where h.userId=:userId");
			query.setLong("userId", userId);
			list = query.list();

		} catch (Exception e) {

			e.printStackTrace();
		} 
		return list;

	}

	private void save(Session session,int id,Value type,double result)
	{
		try{
		 if(type.equals(valueDao.getValue(BusinessConstant.H_CHASSIS)))
		   {
			   Query query=session.createQuery("from Chassis c where c.id="+id);
			   Chassis c=(Chassis) query.uniqueResult();
			   c.setzScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_CPU)))
		   {
			   Query query=session.createQuery("from CPU c where c.id="+id);
			   CPU c=(CPU) query.uniqueResult();
			   c.setzScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_DISK)))
		   {
			   Query query=session.createQuery("from Disk c where c.id="+id);
			   Disk c=(Disk) query.uniqueResult();
			   c.setzScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_DRIVER)))
		   {
			   Query query=session.createQuery("from Driver c where c.id="+id);
			   Driver c=(Driver) query.uniqueResult();
			   c.setzScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_GRAPHICSA)))
		   {
			   Query query=session.createQuery("from Graphics c where c.id="+id);
			   Graphics c=(Graphics) query.uniqueResult();
			   c.setzScore(result);
		   }
		 
		 if(type.equals(valueDao.getValue(BusinessConstant.H_MAINBOARD)))
		   {
			   Query query=session.createQuery("from MainBoard c where c.id="+id);
			   MainBoard c=(MainBoard) query.uniqueResult();
			   c.setzScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_MEMORY)))
		   {
			   Query query=session.createQuery("from Memory c where c.id="+id);
			   Memory c=(Memory) query.uniqueResult();
			   c.setzScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_MONITOR)))
		   {
			   Query query=session.createQuery("from Monitor c where c.id="+id);
			   Monitor c=(Monitor) query.uniqueResult();
			   c.setzScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_NETWORKADAPER)))
		   {
			   Query query=session.createQuery("from NetworkAdapter c where c.id="+id);
			   NetworkAdapter c=(NetworkAdapter) query.uniqueResult();
			   c.setzScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_POWER)))
		   {
			   Query query=session.createQuery("from Power c where c.id="+id);
			   Power c=(Power) query.uniqueResult();
			   c.setzScore(result);
		   }
		 
		 if(type.equals(valueDao.getValue(BusinessConstant.H_RADIATOR)))
		   {
			   Query query=session.createQuery("from Radiator c where c.id="+id);
			   Radiator c=(Radiator) query.uniqueResult();
			   c.setzScore(result);
		   } if(type.equals(valueDao.getValue(BusinessConstant.H_SOUNDBOX)))
		   {
			   Query query=session.createQuery("from SoundBox c where c.id="+id);
			   SoundBox c=(SoundBox) query.uniqueResult();
			   c.setzScore(result);
		   }
		   if(type.equals(valueDao.getValue(BusinessConstant.H_SOUNDCARD)))
		   {
			   Query query=session.createQuery("from SoundCard c where c.id="+id);
			   SoundCard c=(SoundCard) query.uniqueResult();
			   c.setzScore(result);
		   }
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	private void save2(Session session,int id,Value type,double result)
	{
		try{
		 if(type.equals(valueDao.getValue(BusinessConstant.H_CHASSIS)))
		   {
			   Query query=session.createQuery("from Chassis c where c.id="+id);
			   Chassis c=(Chassis) query.uniqueResult();
			   c.setwScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_CPU)))
		   {
			   Query query=session.createQuery("from CPU c where c.id="+id);
			   CPU c=(CPU) query.uniqueResult();
			   c.setwScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_DISK)))
		   {
			   Query query=session.createQuery("from Disk c where c.id="+id);
			   Disk c=(Disk) query.uniqueResult();
			   c.setwScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_DRIVER)))
		   {
			   Query query=session.createQuery("from Driver c where c.id="+id);
			   Driver c=(Driver) query.uniqueResult();
			   c.setwScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_GRAPHICSA)))
		   {
			   Query query=session.createQuery("from Graphics c where c.id="+id);
			   Graphics c=(Graphics) query.uniqueResult();
			   c.setwScore(result);
		   }
		 
		 if(type.equals(valueDao.getValue(BusinessConstant.H_MAINBOARD)))
		   {
			   Query query=session.createQuery("from MainBoard c where c.id="+id);
			   MainBoard c=(MainBoard) query.uniqueResult();
			   c.setwScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_MEMORY)))
		   {
			   Query query=session.createQuery("from Memory c where c.id="+id);
			   Memory c=(Memory) query.uniqueResult();
			   c.setwScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_MONITOR)))
		   {
			   Query query=session.createQuery("from Monitor c where c.id="+id);
			   Monitor c=(Monitor) query.uniqueResult();
			   c.setwScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_NETWORKADAPER)))
		   {
			   Query query=session.createQuery("from NetworkAdapter c where c.id="+id);
			   NetworkAdapter c=(NetworkAdapter) query.uniqueResult();
			   c.setwScore(result);
		   }
		 if(type.equals(valueDao.getValue(BusinessConstant.H_POWER)))
		   {
			   Query query=session.createQuery("from Power c where c.id="+id);
			   Power c=(Power) query.uniqueResult();
			   c.setwScore(result);
		   }
		 
		 if(type.equals(valueDao.getValue(BusinessConstant.H_RADIATOR)))
		   {
			   Query query=session.createQuery("from Radiator c where c.id="+id);
			   Radiator c=(Radiator) query.uniqueResult();
			   c.setwScore(result);
		   } if(type.equals(valueDao.getValue(BusinessConstant.H_SOUNDBOX)))
		   {
			   Query query=session.createQuery("from SoundBox c where c.id="+id);
			   SoundBox c=(SoundBox) query.uniqueResult();
			   c.setwScore(result);
		   }
		   if(type.equals(valueDao.getValue(BusinessConstant.H_SOUNDCARD)))
		   {
			   Query query=session.createQuery("from SoundCard c where c.id="+id);
			   SoundCard c=(SoundCard) query.uniqueResult();
			   c.setwScore(result);
		   }
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.software.cdiy.dao.HcommentDao#saveHcomment(cn.edu.nju.software
	 * .cdiy.model.Hcomment) 保存硬件评论
	 */
	@Override
	public Hcomment saveHcomment(Hcomment comment) {
		Session session = sessionFactory.getCurrentSession();

		Hcomment returnHcomment = null;
		try {

			session.save(comment);

			returnHcomment = (Hcomment) session.load(Hcomment.class,
					comment.getId());
			List<Integer> numberList=null;
			if(comment.getType()==2011){
			    numberList=this.getExpertNumberList(comment.getHardwareId(), comment.getHardwareType());
			    int total=0;
			    int totalPlus=0;
			    for(int i=0;i<5;i++){
			    	total=total+numberList.get(i)*20*(i+1);
			    	totalPlus=totalPlus+numberList.get(i);
			    }
			    double result= ((double)total/totalPlus);
			  save(session,comment.getHardwareId(),comment.getHardwareType(),result);
			  
			}
			else{
				numberList=this.getUserNumberList(comment.getHardwareId(), comment.getHardwareType());
				 int total=0;
				    int totalPlus=0;
				    for(int i=0;i<5;i++){
				    	total=total+numberList.get(i)*20*(i+1);
				    	totalPlus=totalPlus+numberList.get(i);
				    }
				    double result= ((double)total/totalPlus);
				  save2(session,comment.getHardwareId(),comment.getHardwareType(),result);
			}
			

		} catch (Exception e) {


			e.printStackTrace();
		} 
		return returnHcomment;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.software.cdiy.dao.HcommentDao#deleteHComment(cn.edu.nju.software
	 * .cdiy.model.Hcomment)
	 */
	@Override
	public void deleteHComment(Hcomment comment) {
		Session session = sessionFactory.getCurrentSession();
		

		try {
			
			session.delete(comment);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.software.cdiy.dao.HcommentDao#deleteHCommentByID(java.lang
	 * .Long) 删除硬件评论
	 */
	@Override
	public void deleteHCommentByID(long hcommnentid) {
		Session session = sessionFactory.getCurrentSession();
		

		try {
		
			Hcomment comment = (Hcomment) session.load(Hcomment.class,
					hcommnentid);
			session.delete(comment);
		
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
	}

	@Override
	public List<Hcomment> getUserHCommentPage(int hardwareId, Value hardwareType, int page) {
		Session session = sessionFactory.getCurrentSession();

		List<Hcomment> returnInt = null;
		try {

			Query query = session
					.createQuery(" from Hcomment h where h.hardwareId=:hardwareId and h.hardwareType=:hardwareType and h.type=2010");
			query.setLong("hardwareId", hardwareId);
			query.setParameter("hardwareType", hardwareType);
			query.setFirstResult((page - 1) * Constant.PAGE_LENGTH);
			query.setMaxResults(Constant.PAGE_LENGTH);
			returnInt = (List<Hcomment>) query.list();

		} catch (Exception e) {

			e.printStackTrace();
		} 
		return returnInt;
	}

	@Override
	public int getUserHCommentLength(int hardwareId, Value hardwareType) {
		Session session = sessionFactory.getCurrentSession();

		int returnInt = 0;
		try {

			Query query = session
					.createQuery("select count(*) from Hcomment h where h.hardwareId=:hardwareId and h.hardwareType=:hardwareType and h.type=2010");
			query.setLong("hardwareId", hardwareId);
			query.setParameter("hardwareType", hardwareType);
			long test = (Long) query.uniqueResult();
			returnInt = (int) test;
		} catch (Exception e) {

			e.printStackTrace();
		} 
		return returnInt;
	}

	@Override
	public List<Hcomment> getExpertHCommentPage(int hardwareId, Value hardwareType, int page) {
		Session session = sessionFactory.getCurrentSession();

		List<Hcomment> returnInt = null;
		try {

			Query query = session
					.createQuery(" select h from Hcomment h  where h.hardwareId=:hardwareId and h.hardwareType=:hardwareType and h.type=2011");
			query.setLong("hardwareId", hardwareId);
			query.setParameter("hardwareType", hardwareType);

			query.setFirstResult((page - 1) * Constant.PAGE_LENGTH);
			query.setMaxResults(Constant.PAGE_LENGTH);
			returnInt = (List<Hcomment>) query.list();

		} catch (Exception e) {

			e.printStackTrace();
		}
		return returnInt;
	}

	@Override
	public int getExpertHCommentLength(int hardwareId, Value hardwareType) {

		Session session = sessionFactory.getCurrentSession();

		int returnInt = 0;
		try {

			Query query = session
					.createQuery("select count(*) from Hcomment h where h.hardwareId=:hardwareId and h.hardwareType=:hardwareType and h.type=2011");
			query.setLong("hardwareId", hardwareId);
			query.setParameter("hardwareType", hardwareType);
			long test = (Long) query.uniqueResult();
			returnInt = (int) test;
		} catch (Exception e) {

			e.printStackTrace();
		} 
		return returnInt;
	}

	@Override
	public List<Integer> getExpertNumberList(int hardwareId,
			Value hardwareType) {
		Session session = sessionFactory.getCurrentSession();

		List<Integer> list = new ArrayList<Integer>();
		try {

			Query query = session
					.createQuery("select count(*) from Hcomment h where h.hardwareId=:hardwareId and h.hardwareType=:hardwareType and h.type=2011 and h.score=:score");
			query.setLong("hardwareId", hardwareId);
			query.setParameter("hardwareType", hardwareType);
			for (int i = 1; i < 6; i++) {
				query.setInteger("score", 20*i);
				long test = (Long) query.uniqueResult();
				int result = (int) test;
				list.add(result);
			}

		} catch (Exception e) {

			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Integer> getUserNumberList(int hardwareId, Value hardwareType) {
		Session session = sessionFactory.getCurrentSession();

		List<Integer> list = new ArrayList<Integer>();
		try {

			Query query = session
					.createQuery("select count(*) from Hcomment h where h.hardwareId=:hardwareId and h.hardwareType=:hardwareType and h.type=2010 and h.score=:score");
			query.setLong("hardwareId", hardwareId);
			query.setParameter("hardwareType", hardwareType);
			for (int i = 1; i < 6; i++) {
				query.setInteger("score", 20*i);
				long test = (Long) query.uniqueResult();
				int result = (int) test;
				list.add(result);
			}

		} catch (Exception e) {

			e.printStackTrace();
		} 
		return list;
	}

	@Override
	public Hcomment getNewUserHComment(int hardwareId, Value hardwareType) {
		Session session = sessionFactory.getCurrentSession();

		Hcomment returnInt = null;
		try {

			Query query = session
					.createQuery(" select h from Hcomment h  where h.hardwareId=:hardwareId and h.hardwareType=:hardwareType and h.type=2010 order by h.time desc");
			query.setLong("hardwareId", hardwareId);
			query.setParameter("hardwareType", hardwareType);

			query.setFirstResult(0);
			query.setMaxResults(1);
			List<Hcomment> list = query.list();
			if(list.size()!=0)
			{
				returnInt=list.get(0);
			}

		} catch (Exception e) {

			e.printStackTrace();
		} 
		return returnInt;
	}

	@Override
	public Hcomment getNewExpertHComment(int hardwareId, Value hardwareType) {
		Session session = sessionFactory.getCurrentSession();

		Hcomment returnInt = null;
		try {

			Query query = session
					.createQuery(" select h from Hcomment h  where h.hardwareId=:hardwareId and h.hardwareType=:hardwareType and h.type=2011 order by h.time desc");
			query.setLong("hardwareId", hardwareId);
			query.setParameter("hardwareType", hardwareType);

			query.setFirstResult(0);
			query.setMaxResults(1);
			List<Hcomment> list = query.list();
			if(list.size()!=0)
			{
				returnInt=list.get(0);
			}

		} catch (Exception e) {

			e.printStackTrace();
		}
		return returnInt;
	}

}
